package com.henu.engineer.controller.teacher

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.google.gson.JsonObject
import com.henu.engineer.StringUtils
import com.henu.engineer.dao.entity.teachingoutline.CourseMethod
import com.henu.engineer.dao.entity.teachingoutline.CourseTeach
import com.henu.engineer.service.teacher.CourseOutlineService
import org.apache.commons.io.FileUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter


/**
 *
 * @author noexcept
 * @since 2021/8/12 1:15
 */
@RestController
@RequestMapping("/tc")
class CourseOutlineController {

    val logger: Logger = LoggerFactory.getLogger(javaClass)

    @Autowired
    lateinit var courseOutlineService: CourseOutlineService

    /**
     * 返回某老师的所有课程的信息
     */
    @GetMapping("/courseTeachDetails")
    fun courseTeachDetails(teacherId: String?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(teacherId)) {
            res["code"] = 0
            res["msg"] = "blank teacherId"
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "success"
        res["data"] = courseOutlineService.getCourseTeachDetails(teacherId!!)
        return res.toJSONString()
    }

    /**
     * 返回特定课程id的课程信息
     */
    @GetMapping("/courseTeachDetail")
    fun courseTeachDetail(courseId: String?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(courseId)) {
            res["code"] = 0
            res["msg"] = "blank courseId"
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "success"
        res["data"] = courseOutlineService.getCourseTeachDetail(courseId!!)
        return res.toJSONString()
    }

    /**
     * 查看某个课程的所有课程目标
     */
    @GetMapping("/courseGoals")
    fun courseGoals(courseId: String?): String? {
        val res = JSONObject()
        return if (StringUtils.isEmpty(courseId)) {
            res["code"] = 0
            res["msg"] = "courseId is blank"
            res.toJSONString()
        } else {
            res["code"] = 1
            res["msg"] = "success"
            res["res"] = courseOutlineService.getCourseGoalsByCourseId(courseId!!)
            res.toJSONString()
        }
    }

    /**
     * 返回某个课程的所有考核方式详细信息
     */
    @GetMapping("/courseMethod")
    fun courseMethod(courseId: String?): String {
        val res = JSONObject()
        return if (StringUtils.isEmpty(courseId)) {
            res["code"] = 0
            res["msg"] = "courseId is blank"
            res.toJSONString()
        } else {
            res["code"] = 1
            res["msg"] = "success"
            res["res"] = courseOutlineService.getCourseMethods(courseId!!)
            res.toJSONString()
        }
    }

    /**
     * 返回某个课程的课程目标与考核方式的评价标准
     */
    @GetMapping("/goalMethodCriterion")
    fun goalMethodCriterion(courseId: String?): String? {
        val res = JSONObject()
        return if (StringUtils.isEmpty(courseId)) {
            res["code"] = 0
            res["msg"] = "courseId is blank"
            res.toJSONString()
        } else {
            res["code"] = 1
            res["msg"] = "success"
            res["res"] = courseOutlineService.goalMethodCriterion(courseId!!)
            res.toJSONString()
        }
    }

    /**
     * 返回某个课程的所有目标的所有考核方式占比
     */
    @GetMapping("/goalMethodProportion")
    fun goalMethodProportion(courseId: String?): String? {
        val res = JSONObject()
        return if (StringUtils.isEmpty(courseId)) {
            res["code"] = 0
            res["msg"] = "courseId is blank"
            res.toJSONString()
        } else {
            res["code"] = 1
            res["msg"] = "success"
            res["res"] = courseOutlineService.getGoalMethodProportion(courseId!!)
            res.toJSONString()
        }
    }

    /**
     * 下载某课程的课程大纲
     */
    @Deprecated("不用了")
    @GetMapping("/downloadCourseOutlineDoc")
    fun file(courseId: String?): ResponseEntity<ByteArray>? {
        if (StringUtils.isEmpty(courseId)) {
            return null
        }

        //生成文件
        val file = courseOutlineService.generateCourseOutlineDocFile(courseId!!)

        //设置返回给用户所用的文件名
        val dateTime = LocalDateTime.now()
        val formattedTime = DateTimeFormatter.ofPattern(" yyyy-MM-dd-HHmm").format(dateTime)
        val fileName = "$courseId$formattedTime.doc"

        //设置响应头
        val headers = HttpHeaders().apply {
            setContentDispositionFormData("attachment", fileName)
            contentType = MediaType.APPLICATION_OCTET_STREAM
        }

        return try {
            ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK)
        } catch (e: Exception) {
            e.printStackTrace()
            ResponseEntity<ByteArray>(e.message?.encodeToByteArray(), HttpStatus.EXPECTATION_FAILED)
        }
    }


}